import soundfile as sf
import matplotlib.pyplot as plt
from scipy import signal
import numpy as np
import librosa
import os

def get_all_file_path(folder_path):
    file_paths = []

    for root, dirs, files in os.walk(folder_path):
        # 遍历当前文件夹下的所有文件
        for file_name in files:
            file_path = os.path.join(root, file_name)
            file_paths.append(file_path)
    return file_paths

def get_all_file_name(file_paths):
    file_names = []
    for root, dirs, files in os.walk(file_paths):
        for file_name in files:
            file_name = file_name.replace(".wav", "")
            file_names.append(file_name)
    return file_names

def plot_signal(audio_data, title=None):
    plt.figure(figsize=(12, 3.5), dpi=300)
    plt.plot(audio_data, linewidth=1)
    plt.title(title,fontsize = 16)
    plt.tick_params(labelsize=12)
    plt.grid()
    plt.show()

# abnormal normal
folder_path = "C:\\Users\\Administrator\\Desktop\\冠心病\\"

file_paths = get_all_file_path(folder_path)
file_names = get_all_file_name(folder_path)

print(len(file_paths))
print(file_paths)
print(len(file_names))
print(file_names)

for file, file_name in zip(file_paths, file_names):
    audio_data, sr = librosa.load(file, sr=2000)
    plot_signal(audio_data, title=file_name)
